//
//  BgmInputView.m
//  BGM
//
//  Created by liuqin on 2018/12/7.
//  Copyright © 2018 com.alirtc.demo. All rights reserved.
//

#import "BgmInputView.h"
#import "Common.h"

@interface BgmInputView ()<UITextViewDelegate>
@property (nonatomic, strong)UIView *bgView;
@property (nonatomic, strong)UILabel *label;
@property (nonatomic, strong)UIButton *sendBtn;

@end

@implementation BgmInputView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self != nil) {
        self.bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        self.bgView.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.bgView];
        
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, 0.3)];
        lineView.backgroundColor = [UIColor grayColor];
        lineView.alpha = 0.3;
        [self.bgView addSubview:lineView];
        
        self.label = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, frame.size.width-30, frame.size.height-20)];
        self.label.text = @"输入评论";
        self.label.textColor = bgmsmallColor;
        self.label.font = [UIFont systemFontOfSize:15];
        [self.bgView addSubview:self.label];
        
        self.textView = [[UITextView alloc]initWithFrame:CGRectMake(15, 10, frame.size.width-30-70, frame.size.height-20)];
        self.textView.textColor = bgmsmallColor;
        self.textView.returnKeyType = UIReturnKeyDone;
        self.textView.backgroundColor = [UIColor clearColor];
        self.textView.font = [UIFont systemFontOfSize:15];
        self.textView.delegate = self;
        self.textView.layer.cornerRadius = 5;
        self.textView.layer.masksToBounds = YES;
        self.textView.layer.borderColor = bgmsmallColor.CGColor;
        self.textView.layer.borderWidth = 1;
        [self.bgView addSubview:self.textView];
        
        self.sendBtn = [[UIButton alloc]initWithFrame:CGRectMake(frame.size.width-70, 10, 50, frame.size.height-20)];
         self.sendBtn.backgroundColor = bgmyelloeColor;
        [self.sendBtn setTitle:@"发送" forState:UIControlStateNormal];
        [self.sendBtn setTitleColor:bgmmainColor forState:UIControlStateNormal];
        self.sendBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        self.sendBtn.layer.cornerRadius = 5;
        self.sendBtn.layer.masksToBounds = YES;
        [self.bgView addSubview:self.sendBtn];
        
        [self.sendBtn addTarget:self action:@selector(clickBtn) forControlEvents:UIControlEventTouchUpInside];

    }
    return  self;
}

- (void)clickBtn{
    if (self.textView.text.length>0) {
        [self.commonDelegate sendCommonText:self.textView.text];
    }
    [self.textView resignFirstResponder];
}
-(void)textViewDidBeginEditing:(UITextView *)textView{
    self.label.hidden = YES;

}
- (void)textViewDidChange:(UITextView *)textView{
    
    if(textView.text.length==0){
        self.label.hidden = NO;
    }else if (textView.text.length>0){
        self.label.hidden = YES;
    }
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        self.textView.text = textView.text;
        [textView resignFirstResponder];
        return NO;
    }
    if ([textView isFirstResponder]) {
        
        if ([[[textView textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textView textInputMode] primaryLanguage]) {
            return NO;
        }
  
    }
    return YES;
  
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
